import { SaveOutlined, PlusOutlined } from '@ant-design/icons';
import type {ProFormInstance} from '@ant-design/pro-components';
import { ProForm,ProFormText,ProFormItem,ProFormSelect,ProFormRadio} from '@ant-design/pro-components';
import { Space, Button, message,Modal} from 'antd';
import { useRef,useEffect,useState} from 'react';
import {GetUserDetailsRequest,UserInfoSaveRequest} from "@/pages/Dashboard/System/User/service";
import { GetUserDetailsResponse,UserInfoSaveRequestParams} from './data';

import styles from './style.less'
  
  // 添加/编辑人员弹窗组件
  const UserFormModal = ({
    userId,
    show,
    onCancel,
    onSuccess,
   }: {
    userId:string,
    show: boolean;
    onCancel?: () => void;
    onSuccess?:(title:string)=>void;
    }) => {
    const [ loading,setLoading ] = useState(false);
    const [ uid, setUid] = useState(userId)
    const [ data,setData ] = useState();
    const formRef = useRef<ProFormInstance>();
    const doSave = async (values:FormData) => {
    const title:string = values['title'].toString();
    // @ts-ignore
    onSuccess(title);
    useEffect(()=>{
      console.log('hahah')
      // if(userId){
      //     GetUserDetails(userId);
      // }
    },[show])


  //根据用户ID获取用户详情
  //todo: GetUserDetails
  const GetUserDetails = async (id:string) => {
      setLoading(true);
      try {
          const res:AircasKB.ResponseType<GetUserDetailsResponse> = await GetUserDetailsRequest({
              userId:id
          })
          if(res.code == 200){
              setData(res.data);
              formRef.current?.setFieldsValue(res.data)
              console.log('debugger')
          }
          setLoading(false);
      }
      catch (e) {
          setLoading(false);
      }
  }
    //保存用户信息
    const handleSubmit = (e:any) =>{
        formRef.current?.validateFieldsReturnFormatValue?.().then((val) => {
            setLoading(true);
            const requestParams:UserInfoSaveRequestParams = {
              id: id,
              sex:val.sex,
              loginName: val.loginName,
              nickName:val.nickName,
              email:val.email,
              mobile:val.mobile,
              loginDate:val.loginDate
            }
            UserInfoSaveRequest(requestParams).then(res => {
                if(res.code == 200){
                    setLoading(false);
                }
                else{
                    setLoading(false);
                }
            });
        });
    }
  }

    return (
    <Modal
      title={
        <Space>
            添加用户
        </Space>
      }
      open={show}
      centered
      destroyOnClose={true}
      footer={false}
      bodyStyle={{ padding: 10 }}
      onCancel={onCancel}
    >

    
    </Modal>
    );
  };

  export default UserFormModal